<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true"             destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="130px">
			<el-form-item :label="$t('setting.jobs.jobName')" prop="name">
				<el-input v-model="form.name" :setting="$t('all.enter')" clearable></el-input>
			</el-form-item>
			<el-form-item :label="$t('setting.jobs.jobCode')" prop="code">
				<el-input v-model="form.code" :setting="$t('all.enter')" clearable></el-input>
			</el-form-item>
			<el-form-item :label="$t('setting.jobs.role')" prop="role_id">
				<el-select v-model="form.role_id" multiple filterable style="width: 100%">
					<el-option v-for="item in roles" :key="item.id" :label="item.name" :value="item.id" />
				</el-select>
			</el-form-item>
			<el-form-item :label="$t('all.sorts')" prop="sort">
				<el-input-number v-model="form.sort" controls-position="right" :min="1" style="width: 30%;"></el-input-number>
			</el-form-item>
			<el-form-item :label="$t('all.status')" prop="status">
				<el-switch v-model="form.status" :active-value="1" :inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item :label="$t('all.remark')" prop="remark">
				<el-input v-model="form.remark" clearable type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">{{$t('all.cancel')}}</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">{{$t('all.save')}}</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
                add: this.$t('all.add'),
                edit: this.$t('all.edit'),
                show: this.$t('all.show')
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				id: "",
				code: "",
				name: "",
				sort: 1,
				status: 1,
				remark: "",
				role_id: [],
			},
			//验证规则
			rules: {
				sort: [
					{ required: true, message: this.$t('all.enter') + this.$t('setting.dept.sort'), trigger: 'change' }
				],
				name: [
					{ required: true, message: this.$t('all.enter') + this.$t('setting.jobs.jobName') }
				],
				code: [
					{ required: true, message: this.$t('all.enter') + this.$t('setting.jobs.jobCode') }
				],
				role_id: [
					{ required: true, message: this.$t('all.enter') + this.$t('setting.jobs.role')}
				],
			},
			roles: [],
			//所需数据选项
			groups: [],
			groupsProps: {
				value: "id",
				emitPath: false,
				checkStrictly: true
			}
		}
	},
	mounted() {
		this.getGroup()
	},
	methods: {
		//加载树数据
		async getGroup() {
			var res = await this.$API.system.role.list.get();
			this.roles = res.data.rows;
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//加载树数据
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.system.jobs.edit.post(this.form);
					} else {
						res = await this.$API.system.jobs.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success(this.$t('all.yes'))
					} else {
						this.$alert(res.message, this.$t('all.prompt'), {type: "error"})
					}
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.id = data.id
			this.form.name = data.name
			this.form.status = data.status
			this.form.sort = data.sort
			this.form.code = data.code
			this.form.remark = data.remark
			this.form.role_id = data.role_id
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			//Object.assign(this.form, data)
		}
	}
}
</script>

<style></style>
